﻿using Sustainalytics.ClientDW.Synchronization.SyncReferenceUniv.ReferenceUniverseLogic;
using Sustainalytics.Synchronization.Interface;
using System;
using System.Diagnostics;

namespace Sustainalytics.ClientDW.Synchronization.SyncReferenceUniv
{
    public class SyncReferenceUniverses : ISyncStep
    {
        public SyncStepOrder ExecutionOrder => SyncStepOrder.SyncReferenceUniverses;

        public SyncContext Start(SyncContext syncContext)
        {
            var stopWatch = Stopwatch.StartNew();
            try
            {
                var container = SimpleInjectorConfig.Register(new SyncContextLogger(syncContext), syncContext.GAConnectionStrings);
                var referenceUniverseOrchestrator = container.GetInstance<ReferenceUniverseOrchestrator>();
                var insertedUniverses = referenceUniverseOrchestrator.OrchestrateAsync().Result;

                syncContext.Log(
                    $"Reference universes synchronization completed successfully. Rows affected: {insertedUniverses}," +
                    $" Elapsed time: {stopWatch.Elapsed}");
            }
            catch (Exception exception)
            {
                syncContext.Log(exception.ToString());
                syncContext.Status = SyncOperationStatus.Failed;
                throw;
            }

            return syncContext;
        }
    }
}
